package com.yexiong.server.process;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.common.entity.CheckDetail;
import com.common.entity.CheckRecord;
import com.common.entity.DataPack;
import com.common.entity.Parm;
import com.common.server.view.ServerMainFrame;
import com.common.server.view.ServerProcessThread;
import com.common.server.view.ServiceProcess;
import com.sun.jndi.url.iiopname.iiopnameURLContextFactory;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.yexiong.server.dao.ICheckDetailServer;
import com.yexiong.server.dao.ICheckServer;
import com.yexiong.server.dao.impl.CheckDetailServerImpl;
import com.yexiong.server.dao.impl.CheckServerImpl;
import com.zhenhua.dao.IAppointmentDao;
import com.zhenhua.dao.impl.AppointmentDaoImpl;

import jdk.nashorn.internal.parser.DateParser;

public class CheckRecordServiceProcess implements ServiceProcess{
	private ICheckServer ICheckServer=new CheckServerImpl();
	private ICheckDetailServer ICheckDetailServer=new CheckDetailServerImpl();
	private IAppointmentDao IAppointmentDao=new AppointmentDaoImpl();
	
	@Override
	public Object service(DataPack dataPack, ServerProcessThread serverProcessThread, ServerMainFrame serverMainFrame)
			throws IOException {
		if (dataPack.getOptType().equals("show")) {
			Parm parm=(Parm)dataPack.getCon();
			List<CheckRecord> list=ICheckServer.query((String)parm.list.get(0),(String)parm.list.get(1),(String)parm.list.get(2));
			DataPack data=new DataPack();
			
			data.setCon(list);
			serverProcessThread.getOos().writeObject(data);
			serverProcessThread.getOos().flush();
			
		}else if (dataPack.getOptType().equals("deleteRecord")) {
			CheckRecord checkRecord=(CheckRecord)dataPack.getCon();
			String date=new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckDate());
			int checkId=ICheckServer.getId(date, checkRecord.getCheckStuId());
			int id=ICheckDetailServer.delete(date,checkId);
			
			int id1=ICheckServer.delete(date,checkRecord.getCheckStuId());
			int result=id*id1;
			serverProcessThread.getOos().writeObject(result);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("getId")) {
			CheckRecord checkRecord=(CheckRecord)dataPack.getCon();
			String date=new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckDate());
			int checkId=ICheckServer.getId(date, checkRecord.getCheckStuId());
			DataPack dataPack2=new DataPack();
			dataPack2.setCon(checkId);
			serverProcessThread.getOos().writeObject(dataPack2);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("findDate")) {
			List<Object> list=(List<Object>)dataPack.getCon();
			boolean is=ICheckServer.findIsDateIn((int)list.get(0), (String)list.get(1));
			DataPack dataPack2=new DataPack();
			if (is) {
				int checkId=ICheckServer.getId((String)list.get(1), (int)list.get(0));
				dataPack2.setCon(checkId);
				serverProcessThread.getOos().writeObject(dataPack2);
				serverProcessThread.getOos().flush();
			}else {
				dataPack2.setCon(0);
				serverProcessThread.getOos().writeObject(dataPack2);
				serverProcessThread.getOos().flush();
			}
			
		}else if (dataPack.getOptType().equals("getName")) {
			List<String> list=(List<String>)dataPack.getCon();
			list=IAppointmentDao.getSubName(list.get(0), Integer.parseInt(list.get(1)));
			DataPack datapack=new DataPack();
			dataPack.setCon(list);
			serverProcessThread.getOos().writeObject(dataPack);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("add")) {
			CheckRecord checkRecord=(CheckRecord)dataPack.getCon();
			int result=ICheckServer.insert(checkRecord);
			int id=ICheckServer.getId(new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckDate()), checkRecord.getCheckStuId());
			CheckDetail checkDetail=new CheckDetail();
			checkDetail.setDetailCheckId(id);
			checkDetail.setCheckDetailStartTime(checkRecord.getCheckStartTime());
			checkDetail.setCheckDetailEndTime(checkRecord.getCheckOverTime());
			checkDetail.setContent("");
			int re=ICheckDetailServer.insert(checkDetail);
			serverProcessThread.getOos().writeObject((result*re));
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("getParmId")) {
			Parm parm=(Parm)dataPack.getCon();
			CheckRecord checkRecord=(CheckRecord)parm.list.get(0);
			String date=new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckDate());
			int checkId=ICheckServer.getId(date, checkRecord.getCheckStuId());
			CheckDetail checkDetail=(CheckDetail)parm.list.get(1);
			checkDetail.setDetailCheckId(checkId);
			int id=ICheckDetailServer.getId(checkDetail.getDetailCheckId(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDetail.getCheckDetailStartTime()) );
			checkDetail.setCheckDetailId(id);
			serverProcessThread.getOos().writeObject(checkDetail);
			serverProcessThread.getOos().flush();
			
		}
		
		return null;
	}

}
